package com.example.administrator.meishi.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.meishi.R;
import com.example.administrator.meishi.bean.Person;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LoginActivity extends AppCompatActivity {

    private EditText et_user;
    private EditText et_pass;
    private String user;
    private String mpass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    private void initView() {
        et_user = (EditText) findViewById(R.id.et_user);
        et_pass = (EditText) findViewById(R.id.et_pass);
    }

    public void login(View view) {
        user = et_user.getText().toString();
        mpass = et_pass.getText().toString();
        BmobQuery<Person> query1 = new BmobQuery<>();
        query1.addWhereEqualTo("user",user);
        BmobQuery<Person> query2 = new BmobQuery<>();
        query2.addWhereEqualTo("pass",mpass);
        List<BmobQuery<Person>> andQuerys = new ArrayList<BmobQuery<Person>>();
        andQuerys.add(query1);
        andQuerys.add(query2);
        BmobQuery<Person> query = new BmobQuery<Person>();
        query.and(andQuerys);
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }
}
